package com.mantisframework.core.struts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jodd.util.StringUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import com.mantisframework.core.dwz.ServerInfo;
import com.mantisframework.core.page.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionSupport;


/**
 * @作者 马东东
 * @时间：2010-11-11
 * @功能：在struts2框架下获得 HttpServletRequest request;HttpServletResponse
 *                   response;HttpSession session;ServletContext servletContext;
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, ServletContextAware{

	/**
	 * Log log = LogFactory.getLog(BaseAction.class)
	 */
	private static Log log;
	private static final long serialVersionUID = 1L;
	/**
	 * HttpServletRequest request
	 */
	protected HttpServletRequest request;
	/**
	 * HttpServletResponse response
	 */
	protected HttpServletResponse response;
	/**
	 * ServletContext servletContext
	 */
	protected ServletContext servletContext;
	/**
	 * HttpSession session
	 */
	protected HttpSession session;
	private ActionProxy proxy;

	private int statusCode = 200;
	private String message = null;
	private String forwardUrl = null;

	private boolean skipVC = false;
	private String validationCode; // 楠岃瘉鐮�

	// search form fields
	public final static int PAGE_SHOW_COUNT = 20;
	private int pageNum = 1;
	private int numPerPage = 0;
	private String orderField;
	private String orderDirection;
	private String keywords;
	private int totalCount = 0;
	private int totalPage = 1;
	protected static final String OPERATION_DONE = "operationDone";
	
	public ActionProxy getProxy() {
		return proxy;
	}
	
	public String getNamespace(){
		String namespace = proxy.getNamespace();
		if ("/".equals(namespace)) namespace = "";

		return namespace;
	}

	public BaseAction() {
		super();
		log = LogFactory.getLog(this.getClass());
		proxy = ActionContext.getContext().getActionInvocation().getProxy();
	}
	protected boolean uploadFile(File file, String filePath) {

		boolean retCode = false;
		byte[] buffer = new byte[1024];
		FileOutputStream fos = null;
		InputStream is = null;
		try {
			is = new FileInputStream(file);

			fos = new FileOutputStream(new File(filePath));

			int n = -1;
			while ((n = is.read(buffer, 0, buffer.length)) != -1) {
				fos.write(buffer, 0, n);
			}

			retCode = true;
			log.debug("upload file success...");
		} catch (FileNotFoundException fnfe) {
			log.debug("fnfe:" + fnfe);
		} catch (IOException ioe) {
			log.debug("ioe:" + ioe);
		} finally {
			if (fos != null) {
				try {
					fos.close();
					fos = null;
				} catch (IOException e) {
					log.error(e);
				}
			}
			if (is != null) {
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					log.error(e);
				}

			}
		}

		return retCode;
	}
	/**
	 * 
	 * @功能 校验page是否null
	 * @作者 马东东
	 * @时间 2010-11-29 上午11:52:23
	 * @return
	 */
	public PageBean checkPageBean(PageBean pageBean, int pageSize) {
		if (pageBean == null) {
			return new PageBean(1, pageSize);
		} else {
			pageBean.setPageSize(pageSize);
		}
		return pageBean;
	}

	/**
	 * 
	 * @功能 校验page是否null pageSize 默认为10
	 * @作者 马东东
	 * @时间 2010-11-29 上午11:52:23
	 * @return
	 */
	public PageBean checkPageBean(PageBean pageBean) {
		if (pageBean == null) {
			return new PageBean();
		}
		return pageBean;
	}



	/**
	 * 根据参数获取reqeust中的值
	 * 
	 * @param name
	 *            参数名称
	 * @return value值
	 */
	public String getParam(String pName) {
		String v = request.getParameter(pName);
		log.debug("pName=" + v);
		return v == null ? "" : v;
	}

	/**
	 * 
	 * @功能 获得request
	 * @作者 马东东
	 * @时间 2010-11-23 下午03:02:42
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * 
	 * @功能 获得response
	 * @作者 马东东
	 * @时间 2010-11-23 下午03:02:42
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * 获得aplication即ServletContext
	 * 
	 * @return
	 */
	public ServletContext getServletContext() {
		return servletContext;
	}

	/**
	 * 
	 * @功能 获得session
	 * @作者 马东东
	 * @时间 2010-11-23 下午03:02:42
	 * @return
	 */
	public HttpSession getSession() {
		return session;
	}



	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
		this.session = arg0.getSession();
	}

	public void setServletResponse(HttpServletResponse arg0) {
		this.response = arg0;
	}
	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode){
		this.statusCode = statusCode;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}
	protected void margeForwardUrl(String target, String replacement) {
		if (StringUtil.isNotEmpty(forwardUrl) && target != null
				&& replacement != null) {
			forwardUrl = forwardUrl.replace(target, replacement);
		}
	}

	public String getOperationDone() {
		if (ServerInfo.isAjax(request) || request.getParameter("ajax") != null)
			return "ajaxDone";
		else
			return "alert";
	}
	
	private String ajaxForward(int statusCode) {
		this.statusCode = statusCode;
		return OPERATION_DONE;
	}
	protected String ajaxForwardSuccess(String message) {
		this.message = message;
		return ajaxForward(200);
	}
	protected String ajaxForwardError(String message) {
		this.message = message;
		return ajaxForward(300);
	}

	public boolean isSkipVC() {
		return skipVC;
	}

	public void setSkipVC(boolean skipVC) {
		this.skipVC = skipVC;
	}

	public String getValidationCode() {
		return validationCode;
	}

	public void setValidationCode(String validationCode) {
		this.validationCode = validationCode;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setProxy(ActionProxy proxy) {
		this.proxy = proxy;
	}

	/**
	 * @return totalPage
	 */
	
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * @param totalPage 要设置的 totalPage
	 */
	
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}


}
